package com.itheima;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    private ArrayList<Account> accounts = new ArrayList<>();
    private Scanner scanner = new Scanner(System.in);
    private Account loginAcc;

    public void start() {
        while (true) {
            System.out.println("ATM管理系统启动");
            System.out.println("1.用户登录");
            System.out.println("2.用户开户");
            System.out.println("请选择：");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    login();
                    break;
                case 2:
                    creatAccount();
                    break;
                default:
                    System.out.println("输入错误，请重新选择");
            }
        }
    }

    private void login(){
        System.out.println("== 用户登录 ==");
        if(accounts.size() == 0) {
            System.out.println("暂无用户，请先开户");
            return;
        }
        while (true) {
            System.out.println("请输入您的卡号：");
            String cardId = scanner.next();
            Account acc = getAccountByCardId(cardId);
            if (acc == null) {
                System.out.println("卡号不存在，请重新输入");

            }else{
                while (true) {
                    System.out.println("请输入您的密码：");
                    String passWord = scanner.next();
                    if(acc.getPassWord().equals(passWord)){
                        loginAcc = acc;
                        System.out.println("登录成功，欢迎您：" + acc.getUserName()+"成功登录系统,您的卡号是："+acc.getCardId());
                        showUserCommand();
                        return;
                    }else{
                        System.out.println("密码错误，请重新输入");
                    }
                }
            }
        }

    }

    //展示用户登录后的操作界面
    private void showUserCommand(){
        while (true) {
            System.out.println("1.查询余额");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出登录");
            System.out.println("7.注销账户");
            System.out.println("请选择：");
            int command = scanner.nextInt();
            switch (command) {
                case 1:
                    showLoginAccount();
                    break;
                case 2:
                    depositMoney();
                    break;
                case 3:
                    drawMoney();
                    break;
                case 4:
                    transferMoney();
                    break;
                case 5:
                    uodataPassWord();
                    return;
                case 6:
                    System.out.println(loginAcc.getUserName()+"退出登录");
                    return;
                case 7:
                    if(deleteAccount()){
                        return;
                    }
                   break;
                default:
                    System.out.println("输入错误，请重新选择");
            }
        }
    }

    private void uodataPassWord() {
        System.out.println("==修改密码==");
        while (true) {
            System.out.println("请输入您的原密码：");
            String passWord = scanner.next();
            if(loginAcc.getPassWord().equals(passWord)){

                while (true) {
                    System.out.println("请输入您的新密码：");
                    String newPassWord = scanner.next();
                    System.out.println("请再次输入您的新密码：");
                    String okPassWord = scanner.next();
                    if(newPassWord.equals(okPassWord)){
                        loginAcc.setPassWord(newPassWord);
                        System.out.println("修改密码成功");
                        return;
                    }else{
                        System.out.println("输入两次密码不一致，请重新输入");
                    }
                }

            }else {
                System.out.println("密码错误，请重新选择");
            }
        }
    }

    //销户操作
    private boolean deleteAccount() {
        System.out.println("==销户操作==");
        System.out.println("请问您确认销户吗？y\n");
        String command = scanner.next();
        switch (command) {
            case "y":
                if(loginAcc.getMoney() == 0) {
                    accounts.remove(loginAcc);
                    System.out.println("销户成功");
                    return true;
                }else {
                    System.out.println("余额不为0，请先清空余额");
                    return false;
                }
            default:
                System.out.println("输入错误，请重新选择");
                return false;
        }
    }

    //转账
    private void transferMoney() {
        System.out.println("==转账操作==");
        if (accounts.size() < 2) {
            System.out.println("暂无其他用户，请重新选择");
            return;
        }
        //判断自己是否有钱
        if(loginAcc.getMoney() == 0) {
            System.out.println("余额不足，请重新选择");
            return;
        }

        while (true) {
            System.out.println("请输入您要转出的卡号：");
            String cardId = scanner.next();
            Account acc = getAccountByCardId(cardId);
            if (acc == null) {
                System.out.println("卡号不存在，请重新输入");
            } else {
                String name = "*"+acc.getUserName().substring(1);
                System.out.println("请输入"+name+"的姓氏：");
                String preName = scanner.next();
                if(acc.getUserName().startsWith(preName)){
                    while (true) {
                        System.out.println("请输入您转给对方的余额");
                        double money = scanner.nextDouble();
                        if (loginAcc.getMoney() >= money) {
                            loginAcc.setMoney(loginAcc.getMoney()-money);
                            acc.setMoney(acc.getMoney()+money);
                            System.out.println("转账成功，当前余额为："+loginAcc.getMoney());
                            return;
                        } else {
                            System.out.println("余额不足，您账户中余额为："+loginAcc.getMoney()+"元");
                        }
                    }
                }else {
                    System.out.println("姓氏不匹配，请重新输入");

                }
            }
            }
        }


    //取钱
    private void drawMoney() {
        System.out.println("==取钱操作==");
        if (loginAcc.getMoney() < 100) {
            System.out.println("余额不足100元，请重新输入");
            return;
        }
        while (true) {
            System.out.println("请输入您要取出的金额：");
            double money = scanner.nextDouble();
            if (loginAcc.getMoney() >= money) {
                if(money > loginAcc.getLimit()){
                    System.out.println("超出每次取现额度，请重新输入"+loginAcc.getLimit()+"元");
                }else{
                    loginAcc.setMoney(loginAcc.getMoney()-money);
                    System.out.println("取钱成功，当前余额为："+loginAcc.getMoney());
                    break;
                }
            } else {
                System.out.println("余额不足，您账户中余额为："+loginAcc.getMoney()+"元");
            }
        }
    }

    //存钱
    private void depositMoney() {
        System.out.println("==存钱操作==");
        System.out.println("请输入您要存入的金额：");
        double money = scanner.nextDouble();
        loginAcc.setMoney(loginAcc.getMoney()+money);
        System.out.println("存钱成功，当前余额为："+loginAcc.getMoney());
    }


    //展现当前登录账户的信息
    private void showLoginAccount(){
        System.out.println("当前登录账户信息：");
        System.out.println("卡号："+loginAcc.getCardId());
        System.out.println("户主："+loginAcc.getUserName());
        System.out.println("性别："+loginAcc.getSex());
        System.out.println("余额："+loginAcc.getMoney());
        System.out.println("每次取现额度："+loginAcc.getLimit());

    }


    private void creatAccount() {
        Account acc = new Account();

        System.out.println("请您输入您的账户信息:");
        String name = scanner.next();
        acc.setUserName(name);

        while (true) {
            System.out.println("请您输入你的性别：");
            char sex = scanner.next().charAt(0);
            if (sex == '男' || sex == '女') {
                acc.setSex(sex);
                break;
            } else {
                System.out.println("输入错误，请重新选择");
            }
        }

        while (true) {
            System.out.println("请您输入您的密码：");
            String passWord = scanner.next();
            System.out.println("请您确认您的密码：");
            String okPassWord = scanner.next();
            if (passWord.equals(okPassWord)) {
                acc.setPassWord(okPassWord);
                break;
            } else {
                System.out.println("两次密码不一致，请重新输入");
            }

        }

        System.out.println("请您输入您的取现额度");
        double limit = scanner.nextDouble();
        acc.setLimit(limit);

        String cardId = createCardId();
        acc.setCardId(cardId);


        accounts.add(acc);
        System.out.println("恭喜您，" + acc.getUserName() + "开户成功,您的卡号为：" + acc.getCardId());


    }

    private String createCardId() {
        while (true) {
            String cardId = "";

            Random random = new Random();
            for (int i = 0; i < 8; i++) {
                int data = random.nextInt(10);
                cardId += data;
            }

            Account acc = getAccountByCardId(cardId);
            if (acc == null) {
                return cardId;
            }
        }
    }

    private Account getAccountByCardId(String cardId) {
        for (int i = 0; i < accounts.size(); i++) {
            Account acc = accounts.get(i);
            if (acc.getCardId().equals(cardId)) {
                return acc;
            }
        }

        return null;
    }
}
